var STip = ZFT.Util.STip;
var Pagination = require("COMMON/modules/pagination-x/v1.0/");
var ParseTemplate = ZFT.Util.ParseTemplate;
var Ajax = ZFT.Util.Ajax;
var Template = {
    table: ParseTemplate(require("./tpl/table.xtpl")),
    tr: ParseTemplate(require("./tpl/tr.xtpl")),
    detail: ParseTemplate(require("./tpl/detail.xtpl"))
}

var Main = ZFT.Util.Class({
    FEE_TYPE: [
        '房租',
        '物业',
        '水',
        '电',
        '公摊水',
        '公摊电',
        '税费',
        '其他费用',
        /*'优惠金额'*/
    ],
    init: function () {
        var _this = this,
            pagination;
        $("#optBox").html(Template.table());
        pagination = _this.pagination = new Pagination({
            container: $(".paginationWrap"),
            count: 5,
            showTotal: true,
            jump: true,
            filter: true
        });
        pagination.on("page.switch", function (toPage, currentPage, totalPage) {
            pagination.render({ current: currentPage, toPage: toPage }),
                _this.getPayBill(toPage);
        })
        _this.getPayBill(1);
        _this.toggleBillStatus();
    },
    getPayBill: function (toPage,status) {
        if (!toPage) toPage = 1;
        var _this = this,
            tbody = $("#tbody"),
            paginate = $(".paginationWrap"),
            page_size = paginate.attr("data-size"),
            param = {
                status: status ? status : 0,
                page: toPage,
                page_size: page_size ? page_size : 15,
            }
        Ajax("/r/UserCenter_getLeaseBill", {
            type: "POST",
            dataType: "json",
            params: param,
            loading: function () {
                var loading = '<tr><td colspan="8">加载中，请稍候...</td></tr>'
                paginate.hide();
                tbody.html(loading);
            },
            success: function (res) {
                if (res.code == 200) {
                    var html = Template.tr({ data: res.data.list });
                    tbody.html(html);
                    _this.getDetail();
                    _this.pagination.render({ current: toPage, toPage: toPage, total: res.data.total })
                } else {
                    var error = '<tr><td colspan="8">' + res.msg + '</td></tr>';
                    tbody.html(error)
                }
            }
        })
    },
    //获取账单详情
    getDetail: function () {
        var _this = this;
        $("#tbody .btn-getdetail").on("click", function (e) {
            var tarBtn = $(e.currentTarget),
                param = {
                    id: tarBtn.attr("data-id"),
                    status: tarBtn.attr("data-status"),
                    begin_cycle: tarBtn.attr("data-begin"),
                    end_cycle: tarBtn.attr("data-end")
                };
            Ajax("/r/BillManager_leaseBillDetail", {
                type: "POST",
                dataType: "json",
                params:param,
                success: function (res) {
                    if (res.code == 200) {
                        _this.renderBillDetail(res.data);
                    } else {
                        return STip("fail",res.msg);
                    }
                }
            })
        })
    },
    //渲染账单详情
    renderBillDetail: function (data) {
        var _this = this,
            html,
            begin_cycle,
            end_cycle,
            list = data.list;
        for (var i = 0, dataLen = list.length; i < dataLen; i++) {
            list[i].type = _this.FEE_TYPE[list[i].type_money - 1]
        }
        begin_cycle=list[0].begin_cycle,
        end_cycle=list[0].end_cycle;
        html=Template.detail({data:data,begin_cycle:begin_cycle,end_cycle:end_cycle});
        $("#modal").html(html);
    },
    //切换账单的 支付状态
    toggleBillStatus:function(){
        var _this=this;
        $("#billTab .u-box_tab_item").on("click",function(e){
            var tarTab=$(e.currentTarget),
                status=tarTab.attr("data-sts");
            if(tarTab.hasClass("active")) return false;
            tarTab.addClass("active").siblings().removeClass("active");
            _this.getPayBill(1,status);
        })
    }
});
$(function () {
    new Main();
})